package com.logic.landseaserver.ws;

import java.util.Date;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.SysUtil;
import com.logic.landseaserver.common.LandseaErrorMessageEnum;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.common.util.DateUtil;
import com.logic.landseaserver.service.ISendSmsService;
import com.logic.landseaserver.ws.request.GetSendSmsReq;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 短信发送
 *
 * @author Daniel
 * @version 2017年4月18日
 */
@Path("/sendSms")
@Api(value = "sendSms")
public class SendSmsRest {

    @Autowired
    private ISendSmsService sendSmsService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @POST
    @Path("/getVerifyCode")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "get verification code from cellPhone", position = 4)
    public Response getVCode(@ApiParam(value = "param", required = true) GetSendSmsReq verifyReq) {
        CommonResultMap map = new CommonResultMap();
        try {
            if (!verifyReq.getSecretKey().equals(SysUtil.MD5Digest(
                    verifyReq.getPhone() + DateUtil.getInstence().convert(new Date(), "yyyyMMdd") + verifyReq.getOper()))) {
                throw new LandseaBusinessException(LandseaErrorMessageEnum.SMS_GET_VCODE_VALID_SECRETKEY);
            }

            String code = sendSmsService.sendSms(verifyReq);
            //map.setData(code); //Daniel updated unable to back to code
            map.setResult(CommonResult.SUCCESS, "send sms success");
        } catch (BusinessException e) {
            logger.error("getVCode error.", e);
            map.setResult(e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

}
